<?php
namespace app\admin\logic;

class SpaceLogic extends BaseLogic {

    /**
     * 获取分部统计查询条件
     * @param $branchid
     * @return array
     */
    public function get_search_branch_total_maps($branchid){
        $maps = [];
        if($branchid != -1){
            $maps[] = ['id','=',$branchid];
        }
        return $maps;
    }

    /**
     * 获取楼层统计查询条件
     * @param $branchid
     * @param $floorid
     * @return array
     */
    public function get_search_floor_total_maps($branchid,$floorid){
        $maps = [];
        if($branchid != -1){
            $maps[] = ['branchid','=',$branchid];
        }

        if($floorid != -1){
            $maps[] = ['id','=',$floorid];
        }
        return $maps;
    }


    /******************************************************公用方法******************************************************/
    /**
     * 获取工位查询条件
     * @param $branchid
     * @param $floorid
     * @param $use_status
     * @param $work_type
     * @return array
     */
    public function get_search_ws_maps($branchid,$floorid,$use_status,$work_type){
        $maps = [];
        if($branchid != -1){
            $f_maps = [];
            $f_maps[] = ['branchid','=',$branchid];
            $floorids = model('floor')->get_column($f_maps,'id');

            $m_maps = [];
            $m_maps[] = ['floorid','in',$floorids];
            $regionids = model('region')->get_column($m_maps,'id');
            $maps[] = ['regionid','in',$regionids];
        }

        if($floorid != -1){
            $m_maps = [];
            $m_maps[] = ['floorid','=',$floorid];
            $regionids = model('region')->get_column($m_maps,'id');
            $maps[] = ['regionid','in',$regionids];
        }

        if($use_status != -1){
            $now_date = date('Y-m-d');
            if($use_status == 0){
                $ws_maps = [];
                $ws_maps[] = ['company_out_time','<',$now_date];
                $wsids = model('work_station')->get_column($ws_maps,'id');
                $maps[] = ['id','in',$wsids];
            }else{
                $ws_maps = [];
                $wsids = model('work_station')->get_column($ws_maps,'id');
                $crwids = [];
                foreach($wsids as $k => $v){
                    $crw_maps = [];
                    $crw_maps[] = ['wsid','=',$v];
                    $crw_detail = model('cp_region_ws')->get_detail($crw_maps,'id','id desc');
                    $crwids[] = !empty($crw_detail) ? $crw_detail['id'] : 0;
                }
                if($use_status == 1){
                    $crw_maps = [];
                    $crw_maps[] = ['id','in',$crwids];
                    $crw_maps[] = ['use_status','=',$use_status];
                    $wsids = model('cp_region_ws')->get_column($crw_maps,'wsid');

                    $maps[] = ['id','in',$wsids];
                    $maps[] = ['company_out_time','>=',$now_date];
                }elseif($use_status == 2){
                    $crw_maps = [];
                    $crw_maps[] = ['id','in',$crwids];
                    $crw_maps[] = ['use_status','=',1];
                    $wsids = model('cp_region_ws')->get_column($crw_maps,'wsid');

                    $maps[] = ['id','not in',$wsids];
                    $maps[] = ['company_out_time','>=',$now_date];
                }
            }
        }

        if($work_type != -1){
            $maps[] = ['work_type','=',$work_type];
        }
        return $maps;
    }

    /**
     * 统计数据
     * @param $branchid
     * @param $floorid
     * @param $use_status
     * @param $work_type
     * @return mixed
     */
    public function total_ws($branchid,$floorid,$use_status,$work_type){
        $ws_maps = $this->get_search_ws_maps($branchid,$floorid,$use_status,$work_type);
        $wsids = model('work_station')->get_column($ws_maps,'id');

        //获取概述信息
        $s = new \app\common\controller\Space();
        $overview = $s->get_overview($wsids);

        $data['total_num'] = $overview['total_num'];
        $data['vacant_ws_num'] = $overview['vacant_ws_num'];
        $data['person_num'] = $overview['person_num'];
        $data['use_ws_num'] = $overview['use_ws_num'];
        $data['use_rate'] = $overview['use_rate'];
        return $this->get_response(1,'OK',$data);
    }
    /******************************************************公用方法******************************************************/
}